package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     */
    @PostMapping
    Result addDish(@RequestBody DishDTO dishDTO){
        log.info("新增菜品为：{}",dishDTO);
        dishService.addDish(dishDTO);
        //将缓存区中的dish_全部删除
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return Result.success();
    }

    /**
     * 菜品分页查询
     */
    @GetMapping("/page")
    Result getPage(DishPageQueryDTO dishPageQueryDTO){
        log.info("分页查询条件：{}",dishPageQueryDTO);
        PageResult pageResult=dishService.getPage(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 批量删除菜品
     */
    @DeleteMapping
    Result delete(@RequestParam List<Long> ids){
        log.info("要删除的菜品的id：{}",ids);
        dishService.delete(ids);
        //将缓存区中的dish_全部删除
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return Result.success();

    }

    /**
     * 菜品起售，停售
     */
    @PostMapping("/status/{status}")
    Result changeStatus(@PathVariable Integer status,Long id){
        log.info("status:{},id:{}",status,id);
        dishService.changeStatus(status,id);
        //将缓存区中的dish_全部删除
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return Result.success();
    }

    /**
     * 根据id查询菜品
     */
    @GetMapping("/{id}")
    Result getById(@PathVariable Long id){
        log.info("要查询的id：{}",id);
        DishVO dishVO =dishService.getById(id);
        log.info("{}",dishVO);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     */
    @PutMapping
    Result update(@RequestBody DishVO dishVO){
        log.info("修改菜品为：{}",dishVO);
        dishService.update(dishVO);
        //将缓存区中的dish_全部删除
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return Result.success();
    }
    @GetMapping("/list")
    Result<List<Dish>> list(Long categoryId){
        log.info("菜品分类id：{}",categoryId);
        List<Dish> list = dishService.getlist(categoryId);
        return Result.success(list);
    }
}
